package dtu.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.iqengines.demo.R;

import dtu.app.fragment.CategoryListFragment;
import dtu.app.fragment.CategoryListFragment.OnCategoryItemSelectedListener;
import dtu.app.fragment.ProductListFragment;

public class InStockActivity extends FragmentActivity implements OnCategoryItemSelectedListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.instock);
		
		// to give support on lower android version, we are not calling getFragmentManager() 
		FragmentManager fm = getSupportFragmentManager();

		if (fm.findFragmentById(R.id.categoryList) == null) {
			CategoryListFragment list = new CategoryListFragment();
			fm.beginTransaction().add(R.id.categoryList, list).commit();
		}

		if (fm.findFragmentById(R.id.groceryList) == null) {
			ProductListFragment list = new ProductListFragment();
			fm.beginTransaction().add(R.id.groceryList, list).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_scan: startActivity(new Intent(this, ScanActivity.class)); break;
		case R.id.menu_temp_products: startActivity(new Intent(this, TempProductActivity.class)); break;
		default: break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCategoryItemSelected(String category) {
		Log.i("Category", category);
		ProductListFragment list = (ProductListFragment) getSupportFragmentManager().findFragmentById(R.id.groceryList);
		list.updateList(category);
	}
	
}